<?php
App::uses('AppModel', 'Model');
/**
 * User Model
 *
 */
class User extends AppModel {
	public $name = 'User';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'email' => array(
			'email' => array(
				'rule' => array('email'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'password' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'passwd_confirm' => array(
      'required'=>'notEmpty',
      'match'=>array(
        'rule' => 'validatePasswdConfirm',
        'message' => 'Passwords do not match'
      )
    ),
	);

 function validatePasswdConfirm($data){
    if ($this->data['User']['password'] !== $data['passwd_confirm'])
    {
      return false;
    }
    return true;
  }

	public function beforeSave($options = array()) {
		  if (isset($this->data[$this->alias]['password'])) {
		     $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
		  }
		  return true;
	}

}
